package entity;


import enums.ResultCodeEnum;
import lombok.Data;

/**
 * 封装返回结果
 */
@Data
public class Result<T> {

    private Boolean success;

    private String message;

    private T data;

    private Integer code;


    /**
     * 成功不带数据的返回值
     * @return
     */
    public static Result ok(){
        Result result = new Result();
        result.setSuccess(true);
        result.setCode(ResultCodeEnum.SUCCESS.getCode());
        result.setMessage(ResultCodeEnum.SUCCESS.getDesc());
        return result;
    }

    /**
     * 成功带数据的返回值
     * @param data
     * @return
     * @param <T>
     */
    public static <T> Result ok(T data){
        Result result = new Result();
        result.setSuccess(true);
        result.setCode(ResultCodeEnum.SUCCESS.getCode());
        result.setMessage(ResultCodeEnum.SUCCESS.getDesc());
        result.setData(data);
        return result;
    }


    /**
     * 失败不带数据的返回值
     * @return
     */
    public static Result error(){
        Result result = new Result();
        result.setSuccess(false);
        result.setCode(ResultCodeEnum.FAIL.getCode());
        result.setMessage(ResultCodeEnum.FAIL.getDesc());
        return result;
    }


    /**
     * 失败带数据的返回值
     * @param message
     * @return
     */
    public static Result error(String message){
        Result result = new Result();
        result.setSuccess(false);
        result.setCode(ResultCodeEnum.FAIL.getCode());
        result.setMessage(message);
        return result;
    }
}
